<style lang="scss">
  @import "../../styles/base";

  page {
    background-color: #f5f5f5;
  }

  .container {
    height: 100vh;
    .connect-tip {
      padding-top: pxToRem(230);
      padding-bottom: pxToRem(60);
      font-size: pxToRem(50);
      color: #1f1f1f;
      text-align: center;
    }
    .btn-confirm {
      margin-top: pxToRem(100);
      margin-left: pxToRem(170);
      margin-right: pxToRem(170);
      height: pxToRem(120);
      line-height: pxToRem(120);
      text-align: center;
      background-color: $main-color;
      color: $black;
      font-size: pxToRem(50);
      border-radius: pxToRem(60);
    }
    .desc-img {
      display: block;
      margin: pxToRem(100) auto pxToRem(30);
      width: pxToRem(915);
      height: pxToRem(921);
    }
    .wifi-tip {
      font-size: pxToRem(40);
      text-align: center;
      color: $light-black;
      .ssid {
        color: #1f1f1f;
        font-size: pxToRem(40);
      }
    }
  }

  .iphoneX {
    .connect-tip {
      padding-top: pxToRem(250);
      padding-bottom: pxToRem(122);
    }
  }
</style>
<template>
  <div class="container" :class="isIphoneX ? 'iphoneX':''">
    <div class="connect-tip">连接设备工作WIFI</div>
    <img class="desc-img" src="https://image.watsons.com.cn//upload/eb611c69.png">
    <div class="wifi-tip" v-if="SSID">当前WIFI: <span class="ssid">{{SSID}}</span></div>
    <div class="btn-confirm" v-if="isIos" @click="getWifiList">去设置WIFI</div>
  </div>
</template>

<script>

  export default {
    data() {
      return {
        isIphoneX: /iPhone X/.test(App.systemInfo.model),
        isIos: /iOS/.test(App.systemInfo.system),
        SSID: '',
        complete: false,
        jumped: false,
      }
    },
    components: {},
    methods: {
      toConfig() {
        const pages = getCurrentPages();
        if (pages.pop().route === 'pages/wifi-list/main' && this.complete) {
          wx.redirectTo({ url: '/pages/process/main' })
          this.clear();
          return false;
        }
      },
      getWifiList() {
        wx.startWifi({
          success: () => {
            wx.getWifiList();
          }
        })
      },
      async getConnectedWifi() {
        console.info('jumped:' + this.jumped)
        if (this.jumped) {
          return false;
        }
        this.jumped = true;
        let connectResult = await wx.getConnectedWifi();
        this.SSID = connectResult.wifi.SSID;
        console.info('SSID:' + this.SSID)
        App.currentSSID = this.SSID;
        this.complete = /^deerma_300/.test(this.SSID) || /^WTCCN/.test(this.SSID);
        console.log('connectResult:' + JSON.stringify(connectResult))
        if (this.complete && this.jumped) {
          this.toConfig();
          return false;
        }
        this.jumped = false;
      },
      clear() {
        this.complete = false;
        this.jumped = false;
        clearInterval(this.getConnectWifiTask)
        clearInterval(this.wifiTask)
        this.SSID = '';
        this.connectWifi = false;
      },
    },
    onHide() {
      this.clear();
    },
    async onShow() {
      if (!App.userInfo) {
        await App.fetchLogin();
      }
      wx.startWifi({
        success: async () => {
          this.getConnectedWifi();
          wx.onWifiConnected(result => {
            this.SSID = result.wifi.SSID;
            console.info('SSID:' + this.SSID)
            App.currentSSID = this.SSID;
            this.complete = /^deerma_300/.test(this.SSID) || /^WTCCN/.test(this.SSID);
            console.log('connectResult:' + JSON.stringify(result))
            if (this.complete && this.jumped) {
              App.currentSSID = this.SSID;
              this.toConfig();
              return false;
            }
          })
          if (!this.isIos) {
            wx.onGetWifiList(result => {
              if (result.wifiList) {
                result.wifiList.forEach(wifi => {
                  if (this.connectWifi) {
                    return false;
                  }
                  if (/^deerma_300/.test(wifi.SSID) || /^WTCCN/.test(wifi.SSID)) {
                    clearInterval(this.wifiTask)
                    this.connectWifi = true;
                    App.currentSSID = wifi.SSID;
                    wx.connectWifi({
                      SSID: wifi.SSID,
                      password: '',
                      success: () => {
                        this.connectWifi = false;
                      },
                      fail: () => {
                        this.connectWifi = false;
                      },
                      complete: () => {
                        this.connectWifi = false;
                      }
                    })
                  }
                })
              }
            })
            wx.getWifiList();
            this.wifiTask = setInterval(async () => {
              wx.getWifiList();
            }, 2000)
          }
        }
      })
    },
    async mounted() {
    },
  }
</script>
